<?php
require "code/SingleLinkedList.php";

function addTwoNumbers($l1, $l2)
{
    $l3 = null;
    $head = null;
    $status = 0;
    while ($l1 != null || $l2 != null) {
        $value1 = $l1 ? $l1->data : 0;
        $value2 = $l2 ? $l2->data : 0;
        $value = $value1 + $value2 + $status;
        $status = 0;
        if ($value >= 10) {
            $status = 1;
            $value = $value - 10;
        }
        if (!isset($l3)){
            $l3 = $head = new Node($value);
        }else{
            $l3->next = new Node($value);
            $l3 = $l3->next;
        }
        if ($l1){
            $l1 = $l1->next;
        }
        if ($l2){
            $l2 = $l2->next;
        }
        if ($status){
            $l3->next = new Node($status);
        }
    }
    return $head;
}

$l1 = new SingleLinkedList(2);
$l2 = new SingleLinkedList(5);
$l1->insert(2, 4);
$l1->insert(4, 5);

$l2->insert(5, 6);
$l2->insert(6, 4);
$l22 = $l2->find(5);
$l11 = $l1->find(2);
$l3 = addTwoNumbers($l11, $l22);
var_dump($l3);